﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.BusinessLogic.Measures;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Measures;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Common.Utilities;
using DiscoveryLogic.Common.Utilities.OpenXml;

namespace DiscoveryLogic.Analytics.BusinessLogic.Exports
{
    public class ExcelReportUtil
    {
        public static void ExportToExcel(Network network, string excelFilePath)
        {
            DataSet ds=new DataSet(network.NetworkName);
            foreach(EntityType entityType in Enum.GetValues(typeof(EntityType)))
            {
                List<EntityBase> entities = new List<EntityBase>();
                foreach (EntityBase entity in network.Records)
                {
                    if (entity.EntityType ==entityType)
                    {
                        entities.Add(entity);
                    }
                }
                if (entities.Count > 0)
                {
                    DataTable dt = EntityManager.ToTable(entityType, entities);
                    ds.Tables.Add(dt);
                }    
            }

            if (network.Links.Count <= 5000)
            {
                DataTable dtLinks = network.GetLinksAsTable();
                dtLinks.TableName = "Links";
                ds.Tables.Add(dtLinks);
            }

            List<VerticeMeasure> vMeasures = VerticeMeasureManager.GetVerticeMeasures(network.NetworkName);
            DataTable dtVMeasure = VerticeMeasure.ToDataTable(vMeasures);
            dtVMeasure.TableName = "VerticeMeasures";
            ds.Tables.Add(dtVMeasure);

            NetworkMeasureWorker netMeasureWorker=new NetworkMeasureWorker(network);
            NetworkMeasure netMeasure = netMeasureWorker.GetNetworkMeasure();
            DataTable dtGMeasure=new DataTable("GraphMeasure");
            dtGMeasure.Columns.Add("Density", typeof (double));
            dtGMeasure.Columns.Add("EdgeDiameter", typeof(double));
            dtGMeasure.Columns.Add("NodeDiameter", typeof(int));
            DataRow drGMeasure = dtGMeasure.NewRow();
            drGMeasure["Density"] = (double.IsInfinity(netMeasure.Density) || double.IsNaN(netMeasure.Density))
                                        ? -1
                                        : Math.Round(netMeasure.Density, 2);
            drGMeasure["EdgeDiameter"] = (double.IsInfinity(netMeasure.EdgeDiameter) ||
                                          double.IsNaN(netMeasure.EdgeDiameter))
                                             ? -1
                                             : Math.Round(netMeasure.EdgeDiameter, 2);
            drGMeasure["NodeDiameter"] = netMeasure.NodeDiameter;
            dtGMeasure.Rows.Add(drGMeasure);
            ds.Tables.Add(dtGMeasure);

            if(network.Links.Count<=5000)
            {
                DataTable dtRoutes = netMeasure.ToWalkTable();
                if (dtRoutes != null && dtRoutes.Rows.Count <= 5000)
                {
                    dtRoutes.TableName = "Routes";
                    ds.Tables.Add(dtRoutes);
                }    
            }
            
            DataTable dtCommunity = netMeasure.ToCommunityTable();
            dtCommunity.TableName = "Communities";
            ds.Tables.Add(dtCommunity);
            
            ExcelWriter.CreateWorkbook(excelFilePath, ds);
        }
    }
}

